﻿using System;
using System.Collections.Generic;
using System.Text;
using BlueLotus7.Common;
using System.Runtime.Serialization;

namespace BlueLotus7.Contract
{
    [DataContract]
    [KnownType(typeof(ObjectStatus))]
    public class Method : ICloneable, IValidate
    {
        #region Fields

        [DataMember]
        Guid methodKey;
        [DataMember]
        string methodFullName;
        [DataMember]
        string description;
        [DataMember]
        ObjectStatus objectStatus = ObjectStatus.Normal;

        #endregion

        #region Property

        public Guid MethodKey
        {
            get { return methodKey; }
        }

        public string MethodFullName
        {
            get { return methodFullName; }
        }

        public string MethodNamespace
        {
            get
            {
                string source = methodFullName;
                if (!string.IsNullOrWhiteSpace(methodFullName))
                {
                    int index = methodFullName.LastIndexOf('.');
                    if (index > -1)
                    {
                        source = source.Substring(0, index);
                    }
                }
                return source;
            }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        #endregion

        public ObjectStatus ObjectStatus
        {
            get { return objectStatus; }
            set { objectStatus = value; }
        }

        public Method(Guid methodKey, string methodFullName)
        {
            this.methodKey = methodKey;
            this.methodFullName = methodFullName;
        }

        #region ICloneable Members

        public object Clone()
        {
            Method result = new Method(methodKey, methodFullName);
            result.description = description;
            result.objectStatus = objectStatus;
            return result;
        }

        #endregion

        #region IValidate Members

        public bool IsValid
        {
            get
            {
                try
                {
                    Validate();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public void Validate()
        {
            if (methodFullName == null || methodFullName.Length < 3)
            {
                throw new InvalidObjectException("methodFullName");
            }
        }

        #endregion
    }
}
